<?php 
/* ************************* 
 * Script by A. Vladislav I. 
 * ************************* */ 
$newWidth = 200;
$f = $newname;
$type = $_FILES['image']['type']; 
$imgFunc = ''; 
switch($type) 
{ 
    case 'image/gif': 
        $img = ImageCreateFromGIF($f); 
        $imgFunc = 'ImageGIF'; 
        $transparent_index = ImageColorTransparent($img); 
        if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index); 
        break; 
    case 'image/jpeg': 
        $img = ImageCreateFromJPEG($f); 
        $imgFunc = 'ImageJPEG'; 
        break; 
    case 'image/png': 
        $img = ImageCreateFromPNG($f); 
        ImageAlphaBlending($img,true); 
        ImageSaveAlpha($img,true); 
        $imgFunc = 'ImagePNG'; 
        break; 
    default: 
        die("ERROR - no image found"); 
        break; 
}

if(!empty($size)) 
{    
    list($w,$h) = GetImageSize($f);
    
    if( $w<200 or $h==0 )
    {
    	$thumbname = $newname; // image width < 200
    	break; 
    }
    
    $factor = $w / $newWidth;
    $nh = $h / $factor;
    $nw = $newWidth;
    
	// Create new image for thumb
    $img_resized = ImageCreateTrueColor($nw,$nh); 
    
    // handle alpha transparancy
    if($type=='image/png') 
    { 
    	ImageAlphaBlending($img_resized,false); 
    	ImageSaveAlpha($img_resized,true); 
    } 
    if(!empty($transparent_color)) 
    { 
    	$transparent_new = ImageColorAllocate($img_resized,$transparent_color['red'],$transparent_color['green'],$transparent_color['blue']); 
    	$transparent_new_index = ImageColorTransparent($img_resized,$transparent_new); 
    	ImageFill($img_resized, 0,0, $transparent_new_index); 
    }
        
    // Resize
    if(ImageCopyResized($img_resized,$img, 0,0,0,0, $nw,$nh, $w,$h )) 
    { 
    	ImageDestroy($img); 
    	$img = $img_resized; 
		$imgFunc($img, $thumbname); 
		ImageDestroy($img);
    }
    else
    {
    	$thumbname = $newname; // error resizing
		echo '<span style="color: red">Error resizing!</span>';
		$errors=1;
    }
}
else
{
	$thumbname = $newname; // error resizing
	echo '<span style="color: red">Cannot read image size!</span>';
	$errors=1;
}
?> 